/*
 * Created by Steven Jones to create, resize, and position an IFRAME behind a jQuery element.
 */

var autoShimTimeout = null;
var autoShims = new Array();
var autoShimCount = 0;
var autoShimZIndex = 1000;
var autoShimMillis = 100;

var autoPositions = new Array();

function autoShimAttach(jElement) {
    autoShimCount++;
    var shimData = autoShimPositionData(jElement);
    var jShim = $('<iframe id="autoShim'+autoShimCount+'" style="position:absolute;left:'+shimData.x+'px;top:'+shimData.y+'px;width:'+shimData.w+'px;height:'+shimData.h+'px;z-index:'+autoShimZIndex+';border:none">');
    jShim.shimData = shimData;

    // Link elements together
    jShim.autoShimTarget = jElement;
    jElement.autoShim = jShim;

    var z = jElement.css('z-index');
    if (z < autoShimZIndex) {
	jElement.css('z-index', autoShimZIndex + 1);
    }

    // Add shim to list
    autoShims[autoShims.length] = jShim;

    // Add shim to document
    jShim.appendTo('body');

    if (autoShimTimeout == null) {
	autoShimTimeout = setTimeout('autoShimUpdate()', autoShimMillis);
    }
}

function autoShimPositionData(jElement) {
    return {w: jElement.outerWidth(false),
	    h: jElement.outerHeight(false),
	    x: jElement.offset().left,
	    y: jElement.offset().top
	    };
}

function autoShimDetach(jElement) {
    var i = -1;
    for (var j = 0; j < autoShims.length; j++) {
	if (autoShims[j] == jElement.autoShim) {
	    i = j;
	    break;
	}
    }

    if(i != -1) autoShims.splice(i, 1);
    jElement.autoShim.remove();
}

function autoShimUpdate() {
    var count = 0;

    for (var i in autoPositions) {
        count++;
        var e = autoPositions[i];
        var currentPositionData = autoShimPositionData(e);

	var isUpdate = false;

        if (currentPositionData.w != e.shimData.w) {
            e.shimData.w = currentPositionData.w;
	    isUpdate = true;
        }

        if (currentPositionData.h != e.shimData.h) {
            e.shimData.h = currentPositionData.h;
	    isUpdate = true;
        }

        if ((currentPositionData.x != e.shimData.x) || (currentPositionData.y != e.shimData.y)) {
            e.shimData.x = currentPositionData.x;
            e.shimData.y = currentPositionData.y;
	    isUpdate = true;
        }

	if (isUpdate) {
	    var top = e.shimData.y;
	    var left = e.shimData.x + e.shimData.w

	    for (var j in autoPositions[i].autoPositions) {
		var leftOffset = left - autoPositions[i].autoPositions[j].outerWidth(false) - autoPositions[i].autoPositions[j].autoPositionMargins.right;
		var topOffset = top + autoPositions[i].autoPositions[j].autoPositionMargins.top;
		autoPositions[i].autoPositions[j].offset({left:leftOffset, top:topOffset});
	    }
    	}
    }

    for (var i in autoShims) {
	count++;
	var e = autoShims[i];
	var targetShimData = autoShimPositionData(e.autoShimTarget);

	if (e.autoShimTarget.is(":visible")) {
	    if (!e.is(":visible")) {
		e.show();
	    }
	} else {
	    if (e.is(":visible")) {
		e.hide();
		continue;
	    }
	}

	if (targetShimData.w != e.shimData.w) {
	    e.width(targetShimData.w);
	    e.shimData.w = targetShimData.w;
	}
	if (targetShimData.h != e.shimData.h) {
	    e.height(targetShimData.h);
	    e.shimData.h = targetShimData.h;
	}
	
	if ((targetShimData.x != e.shimData.x) || (targetShimData.y != e.shimData.y)) {
	    e.offset({left:targetShimData.x, top:targetShimData.y});
	    e.shimData.x = targetShimData.x;
	    e.shimData.y = targetShimData.y;
	}
    }

    if (count > 0) {
	autoShimTimeout = setTimeout('autoShimUpdate()', autoShimMillis);
    } else {
	clearTimeout(autoShimTimeout);
	autoShimTimeout = null;
    }
}

function autoPositionAttach(jElement, jTargetElement, marginRight, marginTop) {
    if (!("shimData" in jTargetElement)) {
	var shimData = autoShimPositionData(jTargetElement);
	jTargetElement.shimData = shimData;
    }
    if (!("autoPositions" in jTargetElement)) {
	jTargetElement.autoPositions = new Array();
    }
    jTargetElement.autoPositions[jTargetElement.autoPositions.length] = jElement;
    jElement.autoPositionTarget = jTargetElement;
    jElement.autoPositionMargins = {top:marginTop, right:marginRight};

    var top = jTargetElement.shimData.y + jElement.autoPositionMargins.top;
    var left = jTargetElement.shimData.x + jTargetElement.shimData.w - jElement.outerWidth(false) - jElement.autoPositionMargins.right;
    jElement.offset({left:left, top:top});

    // Add position to list
    if ($.inArray(jTargetElement, autoPositions) == -1) {
	autoPositions[autoPositions.length] = jTargetElement;
    }

    if (autoShimTimeout == null) {
        autoShimTimeout = setTimeout('autoShimUpdate()', autoShimMillis);
    }
}
